﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class DinnerExpenseDetailInfos : DetailView<dinnerexpensedetail>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<dinnerexpensedetail> EntityList;
        private new dinnerexpensedetail Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<dinnerexpensedetail>

        public override List<dinnerexpensedetail> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.dinnerexpensedetails.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<dinnerexpensedetail> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.dinnerexpensedetails.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override dinnerexpensedetail GetEntity()
        {
            _context = new project5188Entities();
            Entity = new dinnerexpensedetail();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTodinnerexpensedetails(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<dinnerexpensedetail> GetEntityListByParameters(string registryNoText, string nameSurname, long voucherContentsParameterValueId, DateTime? invoiceReceiptDate)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.personnelinfo.IsDeleted = @PersonnelIsDeleted";
            parameterList.Add(new ObjectParameter("PersonnelIsDeleted", false));

            if (!string.IsNullOrEmpty(registryNoText) && !string.IsNullOrWhiteSpace(registryNoText))
            {
                dynamicString += " And it.personnelinfo.b_RegisterNo = @RegisterNo";
                parameterList.Add(new ObjectParameter("RegisterNo", Convert.ToInt32(registryNoText)));
            }

            if (!string.IsNullOrEmpty(nameSurname) && !string.IsNullOrWhiteSpace(nameSurname))
            {
                dynamicString += " And it.personnelinfo.f_NameSurname = @NameSurname";
                parameterList.Add(new ObjectParameter("NameSurname", nameSurname));
            }

            if (voucherContentsParameterValueId > 0)
            {
                dynamicString += " And it.d_VoucherContentsParameterValueID_15 = @VoucherContentsParameterValueId";
                parameterList.Add(new ObjectParameter("VoucherContentsParameterValueId", voucherContentsParameterValueId));
            }

            if (invoiceReceiptDate != null)
            {
                dynamicString += " And it.j_InvoiceReceiptDate >= @InvoiceReceiptDateStart";
                parameterList.Add(new ObjectParameter("InvoiceReceiptDateStart", new DateTime(invoiceReceiptDate.Value.Year, invoiceReceiptDate.Value.Month, 1)));

                dynamicString += " And it.j_InvoiceReceiptDate <= @InvoiceReceiptDateEnd";
                parameterList.Add(new ObjectParameter("InvoiceReceiptDateEnd", new DateTime(invoiceReceiptDate.Value.Year, invoiceReceiptDate.Value.Month, DateTime.DaysInMonth(invoiceReceiptDate.Value.Year, invoiceReceiptDate.Value.Month))));
            }

            EntityList = _context.dinnerexpensedetails.Where(dynamicString, parameterList.ToArray()).
                OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
